package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.UserMapper;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.UserService;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Long selectUserCount() {
        return userMapper.selectUserCount();
    }

    @Override
    public List<User> selectAllUser(User user) {
        List<User> list = userMapper.selectAllUser(user);
        for(int i=0; i<list.size(); i++){
            list.get(i).setLoginPassword(null);
            list.get(i).setLoginErrorNum(null);
        }
        return list;
    }

    @Override
    public List<User> selectAll() {
        List<User> list = userMapper.selectAll();
        for(int i=0; i<list.size(); i++){
            list.get(i).setLoginPassword(null);
            list.get(i).setLoginErrorNum(null);
        }
        return list;
    }

    @Override
    public User selectUserForLogin(User user) {
        User user1 = userMapper.selectUserForLogin(user);
        if(user1 != null){
            user1.setLoginErrorNum(null);
            user1.setLoginPassword(null);
        }
        return user1;
    }

    @Override
    public PageBean<User> selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex() * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<User> list = userMapper.selectByCondition(criteria);
        for (int i=0; i<list.size(); i++){
            list.get(i).setLoginPassword(null);
            list.get(i).setLoginErrorNum(null);
        }
        Long count = userMapper.selectByConditionCount(criteria);
        pageBean.setTotalCount(count);
        pageBean.setResultList(list);
        return pageBean;
    }

    @Override
    public List<User> selectByConditionForExport(Criteria criteria) {
        List<User> list = userMapper.selectByConditionForExport(criteria);
        for (int i=0; i<list.size(); i++){
            list.get(i).setLoginPassword(null);
            list.get(i).setLoginErrorNum(null);
        }
        return list;
    }

    @Override
    public PageBean<User> selectAllUserNotHaveDepartment(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex() * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<User> list = userMapper.selectAllUserNotHaveDepartment(criteria);
        for (int i=0; i<list.size(); i++){
            list.get(i).setLoginPassword(null);
            list.get(i).setLoginErrorNum(null);
        }
        Long count = userMapper.selectAllUserNotHaveDepartmentCount(criteria);
        pageBean.setTotalCount(count);
        pageBean.setResultList(list);
        return pageBean;
    }

    @Override
    public int deleteDepartment(List list) {
        return userMapper.deleteDepartment(list);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(User record) {
        return 0;
    }

    @Override
    public int insertSelective(User record) {
        record.setLoginPassword("123456a");
        record.setCreateTime(new Date());
        record.setTs(new Date());
        record.setLoginErrorNum(0);
        record.setIsDefaultPassword(1);
        record.setIsOnline(2);
        record.setDr(0);
        return userMapper.insertSelective(record);
    }

    @Override
    public Long selectByUser(User user) {
        return userMapper.selectByUser(user);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        User user =userMapper.selectByPrimaryKey(id);
        user.setLoginPassword(null);
        user.setLoginErrorNum(null);
        return user;
    }

    @Override
    public int updateByList(List<User> list) {
        return userMapper.updateByList(list);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return 0;
    }

    @Override
    public User checkPassword(Long id) {
        User user =userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public User selectUserByLoginName(String loginName) {
        return null;
    }

    @Override
    public int updateLoginErrorNum(String loginName) {
        User user = userMapper.selectUserByLoginName(loginName);
        if(user == null){
            return -1;
        }
        user.setEnableStatus(null);
        user.setLoginErrorNum(user.getLoginErrorNum()+1);
        if(user.getLoginErrorNum() == 5){
            user.setEnableStatus(4);
        }
        int i = userMapper.updateLoginErrorNum(user);
        if(i == 0){
            return -1;
        }
        return user.getLoginErrorNum();
    }
}